using UnityEngine;
using Voodoo.Sauce.Core;
using Voodoo.Sauce.Internal.StoreUtility;
using Voodoo.Sauce.Internal.VoodooTune;

namespace Voodoo.Sauce.Debugger
{
	public static class Debugger
	{
		private static DebuggerCanvas _debugger;

		static Debugger()
		{
			_debugger = Object.FindObjectOfType<DebuggerCanvas>();
			if (_debugger == null)
			{
				_debugger = WidgetUtility.InstantiateDebugger();
			}
		}

		public static void TryOpen()
		{
			bool num = Application.identifier == "io.voodoo.voodoosaucetestapp";
			bool useRemoteConfig = VoodooSettings.Load().UseRemoteConfig;
			bool isDebugBuild = Debug.isDebugBuild;
			bool debuggerAuthorization = VoodooTuneManager.GetDebuggerAuthorization();
			if (!num && useRemoteConfig && !isDebugBuild && !debuggerAuthorization)
			{
				AppInfo.GetAppUpdateStatus(OnAppUpdateStatusReceived);
			}
			else
			{
				_debugger.Open();
			}
		}

		public static void TryClose()
		{
			if ((bool)_debugger)
			{
				_debugger.Close();
			}
		}

		private static void OnAppUpdateStatusReceived(AppUpdateStatus status)
		{
			if (status != AppUpdateStatus.TEST_MODE)
			{
				string message = ((Application.internetReachability == NetworkReachability.NotReachable) ? "An error occurred, cannot retrieve data.\nPlease Verify you have access to the internet." : ((status != AppUpdateStatus.UNKNOWN) ? "The game version must be higher than the store version or your ID must be added to VoodooTune test devices." : "The game isn't found in the store, your ID must be added to VoodooTune test devices."));
				DisplayPopup(new DebuggerPopupConfig
				{
					message = message,
					showCancelButton = false,
					confirmText = "Close",
				});
			}
			else
			{
				_debugger.Open();
			}
		}

		public static void Show(Screen screen)
		{
			if (!_debugger.IsOpened)
			{
				_debugger.Open();
			}
			_debugger.Push(screen);
		}

		public static void Previous()
		{
			if (_debugger.IsHome)
			{
				_debugger.Close();
			}
			else
			{
				_debugger.Pop();
			}
		}

		public static void Toggle(Screen screen)
		{
			if (!_debugger.IsOpened)
			{
				_debugger.Open();
			}
			_debugger.Toggle(screen);
		}

		internal static void DisplayPopup(string message)
		{
			DisplayPopup(new DebuggerPopupConfig
			{
				message = message,
				showCancelButton = false,
				confirmText = "Ok"
			});
		}

		internal static void DisplayPopup(string title, string message)
		{
			DisplayPopup(new DebuggerPopupConfig
			{
				title = title,
				message = message,
				showCancelButton = false,
				confirmText = "Ok"
			});
		}

		internal static void DisplayPopup(DebuggerPopupConfig config)
		{
			if ((bool)_debugger)
			{
				_debugger.ShowPopup(config);
			}
		}
	}
}
